#!/bin/sh

. /usr/share/common

nfs_mount=/mnt/nfs

start() {
	echo_title "Mount $nfs_mount"

	nfs_share=$(fw_printenv -n nfs_share)
	if [ -z "$nfs_share" ]; then
		echo_info "Mount share is not set"
		return 0
	fi

	if mountpoint -q "$nfs_mount"; then
		echo_error "$nfs_mount already mounted"
		exit 1
	fi

	if mount -o nolock "$nfs_share" "$nfs_mount"; then
		echo_info "Mounted $nfs_share on $nfs_mount"
	else
		echo_error "Failed to mount $nfs_share on $nfs_mount"
		exit 1
	fi
}

stop() {
	echo_title "Unmount $nfs_mount"

	if ! mountpoint -q "$nfs_mount"; then
		echo_info "$nfs_mount is not mounted"
		return 0
	fi

	if umount -l "$nfs_mount"; then
		echo_info "$nfs_mount unmounted"
	else
		echo_error "Failed to unmount $nfs_share"
		exit 1
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		sleep 2
		start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
		;;
esac

exit 0

